'use strict';
var config = require('../config');
var JsonDataStr = require('./jsonDataStr');

exports.manager_card_list_page = function(req, res) {
	var AppId = config.AppId;
	var page = req.query.page;
	var size = req.query.size;
	if (!page) {
		page = 1;
	}
	if (!size) {
		size = 10;
	}
	var path = '/v1/manager/imageShare/list/display?AppId=' + AppId + '&display=true&page=' + page + '&size=' + size;
	JsonDataStr.get(path, function(callback) {
		if (!callback.status) {
			return res.render('admin/msg', {
				title: '推荐卡片列表',
				userinfo: req.session.sys_user,
				info: '系统错误:' + callback.message,
				btnOKUrl: null,
				btnOKName: null,
				btnCancelUrl: '/admin/index',
				btnCancelName: '返回'
			});
		}
		res.render('admin/card', {
			title: '推荐卡片列表',
			userinfo: req.session.sys_user,
			cards: callback.resources,
			total: callback.total,
			thisPage: page
		});
	});
}

exports.manager_card_list_display_page = function(req, res) {
	var AppId = config.AppId;
	var page = req.query.page;
	var size = req.query.size;
	if (!page) {
		page = 1;
	}
	if (!size) {
		size = 10;
	}
	//临时代码 2018年7月31日19:33:58
	if(page>300){
		page = page-300;
	}
	var path = '/v1/manager/imageShare/list/display?AppId=' + AppId + '&page=' + page + '&size=' + size;
	JsonDataStr.get(path, function(callback) {
		if (!callback.status) {
			return res.render('admin/msg', {
				title: '分享卡片列表',
				userinfo: req.session.sys_user,
				info: '系统错误:' + callback.message,
				btnOKUrl: null,
				btnOKName: null,
				btnCancelUrl: '/admin/index',
				btnCancelName: '返回'
			});
		}
		res.render('admin/card_display', {
			title: '分享卡片列表',
			userinfo: req.session.sys_user,
			cards: callback.resources,
			total: callback.total+2542,
			thisPage: page
		});
	});
}

exports.manager_card_add_page = function(req, res) {
	res.render('admin/cardAdd', {
		title: '新增card',
		userinfo: req.session.sys_user
	})
}

//添加card
exports.manager_card_add = function(req, res) {
	var AppId = config.AppId;
	var cardTitle = req.body.cardTitle;
	var cardMessage = req.body.cardMessage;
	var display = req.body.display;
	var cardUrl = req.body.cardUrl;
	var indexNum = req.body.indexNum;
	var postData = JSON.stringify({
		AppId: AppId,
		cardTitle: cardTitle,
		cardMessage: cardMessage,
		cardUrl: cardUrl,
		indexNum: indexNum,
		display: display
	});
	var path = '/v1/imageShare/add';
	JsonDataStr.post(postData, path, function(row) {
		if (!row.status) {
			return res.json({
				status: false,
				message: '系统错误:' + row.message
			});
		}
		res.json({
			status: true,
			resources: row.resources
		});
	});
};

exports.manager_card_edit_page = function(req, res) {
	var imageShare_id = req.query.imageShare_id;
	if (!imageShare_id) {
		return res.json({
			status: false,
			message: 'no imageShare_id'
		});
	}
	var path = '/v1/imageShare/find?imageShare_id=' + imageShare_id;
	JsonDataStr.get(path, function(row) {
		if (!row.status) {
			return res.json({
				status: false,
				message: '系统错误:' + row.message
			});
		}
		res.render('admin/cardEdit', {
			title: '修改card',
			userinfo: req.session.sys_user,
			card: row.resources
		});
	});
}

//系统管理员修改card内容
exports.manager_card_edit = function(req, res) {
	var imageShare_id = req.body.imageShare_id;
	if (!imageShare_id) {
		return res.json({
			status: false,
			message: 'no imageShare_id'
		});
	}
	var cardTitle = req.body.cardTitle;
	var cardMessage = req.body.cardMessage;
	var cardUrl = req.body.cardUrl;
	var indexNum = req.body.indexNum;
	var postData = JSON.stringify({
		imageShare_id: imageShare_id,
		cardTitle: cardTitle,
		cardMessage: cardMessage,
		cardUrl: cardUrl,
		indexNum: indexNum
	});
	var path = '/v1/manager/imageShare/edit';
	JsonDataStr.post(postData, path, function(row) {
		if (!row.status) {
			return res.json({
				status: false,
				message: '系统错误'
			});
		}
		res.json({
			status: true
		});
	})
}

//系统管理员删除card
exports.manager_card_del = function(req, res) {
	var imageShare_id = req.body.imageShare_id;
	if (!imageShare_id) {
		return res.json({
			status: false,
			message: 'no imageShare_id'
		});
	}
	var path = '/v1/manager/imageShare/delete';
	var postData = JSON.stringify({
		imageShare_id: imageShare_id
	});
	JsonDataStr.post(postData, path, function(row) {
		if (!row.status) {
			return res.json({
				status: false,
				message: '系统错误'
			});
		}
		res.json({
			status: true
		});
	})
}

//系统管理员推荐卡片
exports.manager_card_recommend = function(req, res) {
	var imageShare_id = req.body.imageShare_id;
	if (!imageShare_id) {
		return res.json({
			status: false,
			message: 'no imageShare_id'
		});
	}
	var type = req.body.type;
	if (!type) {
		type = false;
	} else {
		type = true;
	}
	var path = '/v1/manager/imageShare/recommend';
	var postData = JSON.stringify({
		imageShare_id: imageShare_id,
		type: type
	});
	JsonDataStr.post(postData, path, function(row) {
		if (!row.status) {
			return res.json({
				status: false,
				message: '系统错误'
			});
		}
		res.json({
			status: true
		});
	})
}